package debugServer;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import YAPserver.logic.Game;

public class GlobalModPanel extends JPanel{
	GlobalModCode selection;
	Game game;
	
	JButton addButton = new JButton("add");
	JButton activateButton = new JButton("activate");
	JList<GlobalModCode> modList = new JList<GlobalModCode>(GlobalModCode.values());
	JScrollPane modPane = new JScrollPane(modList);
	JPanel buttonPanel = new JPanel(new BorderLayout());
	
	GlobalModPanel(Game game){
		this.game = game;
		createPanel();
	}
	
	private void createPanel(){
		this.setLayout(new GridLayout(1, 4));
		
		addButton.addActionListener(new AddListener());
		activateButton.addActionListener(new ActivateListener());
		buttonPanel.setLayout(new GridLayout(2,1));
		buttonPanel.add(addButton);
		buttonPanel.add(activateButton);
		
		modPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
		modPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		modList.setVisibleRowCount(4);
		modList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		modList.setSelectedIndex(0);
		modList.addListSelectionListener(new ListListener());
		
		this.add(modPane, BorderLayout.WEST);
		this.add(new JPanel());
		this.add(new JPanel());
		this.add(buttonPanel, BorderLayout.EAST);
		
		selection = modList.getSelectedValue();
	}
	
	class ActivateListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			DebugConsole.activate(selection, game);
		}
		
	}
	
	class AddListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			DebugConsole.addMod(selection, game);
		}
		
	}
	
	class ListListener implements ListSelectionListener{

		@Override
		public void valueChanged(ListSelectionEvent e) {
			selection = modList.getSelectedValue();
		}
		
	}
}
